//
//  PopTransitionAnimation.m
//  29_TransitionAnimation
//
//  Created by 徐康 on 16/8/23.
//  Copyright © 2016年 KX. All rights reserved.
//

#import "PopTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"
@implementation PopTransitionAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.5f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    SecondViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    ViewController *toVC  = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containView = [transitionContext containerView];
    [containView addSubview:toVC.view];
    toVC.imageview.hidden = YES;
    // 截图大法
    UIView *snapView = [fromVC.fromSecondVCImageView snapshotViewAfterScreenUpdates:YES];
    
    snapView.frame = fromVC.fromSecondVCImageView.frame;
    [containView addSubview:snapView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        [containView layoutIfNeeded];
        snapView.frame = toVC.imageview.frame;
        
    } completion:^(BOOL finished) {
        toVC.imageview.hidden = NO;
        [snapView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
    
    
    
}
@end
